home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / AIAT / Headers / Common / Progress.h < prev   
Encoding:
Text File  |  1998-04-16  |  1.9 KB  |  90 lines  |  [TEXT/CWIE]

  1. // Progress.h
  2. //    Copyright:    © 1994 - 1998 by Apple Computer, Inc., all rights reserved.
  3.  
  4.  
  5. // a utility to simplify the implementation of progress reporting
  6.  
  7. #pragma once
  8. #ifndef Progress_h
  9. #define Progress_h
  10.  
  11. #pragma import on
  12.  
  13. #if PRAGMA_STRUCT_ALIGN
  14.     #pragma options align=power
  15. #endif
  16.  
  17.  
  18. #include "IACommon.h"
  19.  
  20. #include <time.h>
  21.  
  22. #pragma IA_BEGIN_EXPORTS
  23.  
  24. /// To use, first create a progress object:
  25. //      Progess progress(updateFreq, progressFunction, data);
  26. //  then create a Phase for each phase of the computation (phases may be nested)
  27. //      Phase thisPhase(<steps>, &progress);
  28. //    for each step of each phase (& sub-phase):
  29. //      progress.Step();
  30. //    and periodically:
  31. //      progress.Check() ? return;
  32. //  the above may be combined with:
  33. //        progress.StepCheck();
  34.  
  35. class Progress;
  36.  
  37. class Phase {
  38. public:
  39.                 Phase(uint32 n, Progress* p);
  40.                 ~Phase();
  41.  
  42.     uint32        atStep;
  43.     uint32        outOf;
  44.     Phase*        parent;                                    // up-link in doubly-linked stack
  45.     Phase*        child;                                    // down-link in doubly linked stack
  46.     Progress*     progress;
  47. private:
  48.                 Phase(Phase&);                            // leave copy ctor undefined
  49.     void*        operator new(size_t size);                // stack allocate only
  50. };
  51.  
  52. typedef bool ProgressFn(float percent, void* data);
  53.  
  54. const float        ProgressEpsilon = 0.001f;
  55.  
  56. class Progress {
  57. public:
  58.                 Progress(clock_t fr, ProgressFn* f, void* d);
  59.     void        Step(uint32 increment = 1) { top->atStep += increment; }
  60.     bool        Check();
  61.     bool        StepCheck(uint32 i = 1) { Step(i); return Check(); }
  62.     void        Complete();
  63.  
  64.     Phase*        top;                                    // top of stack
  65.     Phase*        root;                                    // bottom of stack
  66.  
  67.     void*        data;
  68. private:
  69.  
  70.     clock_t        updateFreq;
  71.     clock_t        lastUpdateTime;
  72.     float        lastUpdatePercent;
  73.     ProgressFn*    fn;
  74.     
  75.     float        PercentComplete();
  76.  
  77.                 Progress(Progress&);                    // leave copy ctor undefined
  78.     void*        operator new(size_t size);                // stack allocate only
  79. };
  80.  
  81. #pragma IA_END_EXPORTS
  82.  
  83. #if PRAGMA_STRUCT_ALIGN
  84.     #pragma options align=reset
  85. #endif
  86.  
  87. #pragma import reset
  88.  
  89. #endif
  90.